package thread;

// 匿名内部类 实现 实现Runnable接口的方式

public class Demo4 {
    public static void main(String[] args) {
        /*Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        // 注意：是在Runnable接口中重写run方法
        // 因为线程的执行和线程实体是区分开的！
        Thread thread = new Thread(runnable);
        thread.start();*/

        // 再进行简化书写
        // 匿名内部类的实例作为构造方法的参数！
        Thread thread = new Thread(new Runnable() {
            // 同样是在Runnable中重写润方法，区分线程实现与线程体
            @Override
            public void run() {
                while (true) {
                    System.out.println("hello thread!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();

        while (true) {
            System.out.println("hello main");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
